package cn.macswelle.campusecurity.raspberry1.controller;

import cn.macswelle.campusecurity.common.dto.responseDto.HttpResult;
import cn.macswelle.campusecurity.raspberry1.service.CODetection;
import cn.macswelle.campusecurity.raspberry1.service.FireDetection;
import cn.macswelle.campusecurity.raspberry1.service.SmokeDetection;
import cn.macswelle.campusecurity.sdk.controller.IndexController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RaspiIndexController extends IndexController {
  @Qualifier("getSDThread")
  @Autowired
  private Thread sDThread;

  @Qualifier("getCODThread")
  @Autowired
  private Thread cODThread;

  @Qualifier("getFDThread")
  @Autowired
  private Thread fDThread;

  @Autowired
  private CODetection coDetection;
  @Autowired
  private FireDetection fireDetection;
  @Autowired
  private SmokeDetection smokeDetection;

  @RequestMapping("/")
  public ModelAndView index() {
    ModelAndView modelAndView = getIndexModelAndWiew();
    modelAndView.addObject("info", info);
    return modelAndView;
  }

  @PostMapping("/pauseAll")
  @ResponseBody
  public HttpResult pauseAll() {
    fireDetection.paused = true;
    coDetection.paused = true;
    smokeDetection.paused = true;
    return new HttpResult("result:success", "msg:暂停成功");
  }

  @PostMapping("/restartAll")
  @ResponseBody
  public HttpResult restartAll() {
    fireDetection.resumeThread();
    coDetection.resumeThread();
    smokeDetection.resumeThread();
    return new HttpResult("result:success", "msg:重启成功");
  }

  @PostMapping("/check")
  @ResponseBody
  public HttpResult checkStatus() {
    System.out.println(sDThread.getState());
    return new HttpResult("smoke:" + sDThread.getState() + " 是否报警" + smokeDetection.input.isLow(),
      "co:" + cODThread.getState() + " 是否报警" + coDetection.input.isLow(),
      "fire:" + fDThread.getState() + " 是否报警" + fireDetection.input.isLow());
  }
}
